<a href='http://github.com/angular/angular.js/edit/master/src/ng/directive/input.js' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this doc</a>



<a href='http://github.com/angular/angular.js/tree/master/src/ng/directive/input.js#L908' class='view-source pull-right btn btn-primary'>
  <i class="glyphicon glyphicon-zoom-in">&nbsp;</i>View Source
</a>


<header class="api-profile-header">
  <h1 class="api-profile-header-heading">ngModel.NgModelController</h1>
  <ol class="api-profile-header-structure naked-list step-list">
    
    <li>
      - type in module <a href="api/ng">ng</a>
    </li>
  </ol>
</header>



<div class="api-profile-description">
  <p><code>NgModelController</code> provides API for the <code>ng-model</code> directive. The controller contains
services for data-binding, validation, CSS updates, and value formatting and parsing. It
purposefully does not contain any logic which deals with DOM rendering or listening to
DOM events. Such DOM related logic should be provided by other directives which make use of
<code>NgModelController</code> for data-binding.</p>
<h2 id="custom-control-example">Custom Control Example</h2>
<p>This example shows how to use <code>NgModelController</code> with a custom control to achieve
data-binding. Notice how different directives (<code>contenteditable</code>, <code>ng-model</code>, and <code>required</code>)
collaborate together to achieve the desired result.</p>
<p>Note that <code>contenteditable</code> is an HTML5 attribute, which tells the browser to let the element
contents be edited in place by the user.  This will not work on older browsers.</p>
<p>We are using the <a href="api/ng/service/$sce">$sce</a> service here and include the <a href="api/ngSanitize">$sanitize</a>
module to automatically remove &quot;bad&quot; content like inline event listener (e.g. <code>&lt;span onclick=&quot;...&quot;&gt;</code>).
However, as we are using <code>$sce</code> the model can still decide to to provide unsafe content if it marks
that content using the <code>$sce</code> service.</p>
<p>

<div>
  <a ng-click="openPlunkr('examples/example-NgModelController')" class="btn pull-right">
    <i class="glyphicon glyphicon-edit">&nbsp;</i>
    Edit in Plunker</a>
  <div class="runnable-example"
      path="examples/example-NgModelController"
      name="NgModelController"
      module="customControl"
      deps="angular-sanitize.js">

   
    <div class="runnable-example-file" 
      name="style.css"
      language="css"
      type="css">
      <pre><code>[contenteditable] {&#10;  border: 1px solid black;&#10;  background-color: white;&#10;  min-height: 20px;&#10;}&#10;&#10;.ng-invalid {&#10;  border: 1px solid red;&#10;}</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="script.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;customControl&#39;, [&#39;ngSanitize&#39;]).&#10;  directive(&#39;contenteditable&#39;, [&#39;$sce&#39;, function($sce) {&#10;    return {&#10;      restrict: &#39;A&#39;, // only activate on element attribute&#10;      require: &#39;?ngModel&#39;, // get a hold of NgModelController&#10;      link: function(scope, element, attrs, ngModel) {&#10;        if(!ngModel) return; // do nothing if no ng-model&#10;&#10;        // Specify how UI should be updated&#10;        ngModel.$render = function() {&#10;          element.html($sce.getTrustedHtml(ngModel.$viewValue || &#39;&#39;));&#10;        };&#10;&#10;        // Listen for change events to enable binding&#10;        element.on(&#39;blur keyup change&#39;, function() {&#10;          scope.$apply(read);&#10;        });&#10;        read(); // initialize&#10;&#10;        // Write data to the model&#10;        function read() {&#10;          var html = element.html();&#10;          // When we clear the content editable the browser leaves a &lt;br&gt; behind&#10;          // If strip-br attribute is provided then we strip this out&#10;          if( attrs.stripBr &amp;&amp; html == &#39;&lt;br&gt;&#39; ) {&#10;            html = &#39;&#39;;&#10;          }&#10;          ngModel.$setViewValue(html);&#10;        }&#10;      }&#10;    };&#10;  }]);</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="index.html"
      language="html"
      type="html">
      <pre><code>&lt;form name=&quot;myForm&quot;&gt;&#10; &lt;div contenteditable&#10;      name=&quot;myWidget&quot; ng-model=&quot;userContent&quot;&#10;      strip-br=&quot;true&quot;&#10;      required&gt;Change me!&lt;/div&gt;&#10;  &lt;span ng-show=&quot;myForm.myWidget.$error.required&quot;&gt;Required!&lt;/span&gt;&#10; &lt;hr&gt;&#10; &lt;textarea ng-model=&quot;userContent&quot;&gt;&lt;/textarea&gt;&#10;&lt;/form&gt;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="protractor.js"
      type="protractor"
      language="js">
      <pre><code>it(&#39;should data-bind and become invalid&#39;, function() {&#10;if (browser.params.browser == &#39;safari&#39; || browser.params.browser == &#39;firefox&#39;) {&#10;  // SafariDriver can&#39;t handle contenteditable&#10;  // and Firefox driver can&#39;t clear contenteditables very well&#10;  return;&#10;}&#10;var contentEditable = element(by.css(&#39;[contenteditable]&#39;));&#10;var content = &#39;Change me!&#39;;&#10;&#10;expect(contentEditable.getText()).toEqual(content);&#10;&#10;contentEditable.clear();&#10;contentEditable.sendKeys(protractor.Key.BACK_SPACE);&#10;expect(contentEditable.getText()).toEqual(&#39;&#39;);&#10;expect(contentEditable.getAttribute(&#39;class&#39;)).toMatch(/ng-invalid-required/);&#10;});</code></pre>
    </div>
  

    <iframe class="runnable-example-frame" src="examples/example-NgModelController/index.html" name="example-NgModelController"></iframe>
  </div>
</div>

</p>

</div>




<div>
  

    

  

  
<h2>Methods</h2>
<ul class="methods">
  <li id="$render">
    <h3><p><code>$render();</code></p>

</h3>
    <div><p>Called when the view needs to be updated. It is expected that the user of the ng-model
directive will implement this method.</p>
</div>

    

    
    
    

  </li>
  
  <li id="$isEmpty">
    <h3><p><code>$isEmpty(value);</code></p>

</h3>
    <div><p>This is called when we need to determine if the value of the input is empty.</p>
<p>For instance, the required directive does this to work out if the input has data or not.
The default <code>$isEmpty</code> function checks whether the value is <code>undefined</code>, <code>&#39;&#39;</code>, <code>null</code> or <code>NaN</code>.</p>
<p>You can override this for input directives whose concept of being empty is different to the
default. The <code>checkboxInputType</code> directive does this because in its case a value of <code>false</code>
implies empty.</p>
</div>

    
    <h4>Parameters</h4>
    
<table class="variables-matrix input-arguments">
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    
    <tr>
      <td>
        value
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-object">*</a>
      </td>
      <td>
        <p>Reference to check.</p>

        
      </td>
    </tr>
    
  </tbody>
</table>

    

    
    
    
    <h4>Returns</h4>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-boolean">boolean</a></td>
    <td><p>True if <code>value</code> is empty.</p>
</td>
  </tr>
</table>
    

  </li>
  
  <li id="$setValidity">
    <h3><p><code>$setValidity(validationErrorKey, isValid);</code></p>

</h3>
    <div><p>Change the validity state, and notifies the form when the control changes validity. (i.e. it
does not notify form if given validator is already marked as invalid).</p>
<p>This method should be called by validators - i.e. the parser or formatter functions.</p>
</div>

    
    <h4>Parameters</h4>
    
<table class="variables-matrix input-arguments">
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    
    <tr>
      <td>
        validationErrorKey
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-string">string</a>
      </td>
      <td>
        <p>Name of the validator. the <code>validationErrorKey</code> will assign
       to <code>$error[validationErrorKey]=!isValid</code> so that it is available for data-binding.
       The <code>validationErrorKey</code> should be in camelCase and will get converted into dash-case
       for class name. Example: <code>myError</code> will result in <code>ng-valid-my-error</code> and <code>ng-invalid-my-error</code>
       class and can be bound to as  <code>{{someForm.someControl.$error.myError}}</code> .</p>

        
      </td>
    </tr>
    
    <tr>
      <td>
        isValid
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-boolean">boolean</a>
      </td>
      <td>
        <p>Whether the current state is valid (true) or invalid (false).</p>

        
      </td>
    </tr>
    
  </tbody>
</table>

    

    
    
    

  </li>
  
  <li id="$setPristine">
    <h3><p><code>$setPristine();</code></p>

</h3>
    <div><p>Sets the control to its pristine state.</p>
<p>This method can be called to remove the &#39;ng-dirty&#39; class and set the control to its pristine
state (ng-pristine class).</p>
</div>

    

    
    
    

  </li>
  
  <li id="$setViewValue">
    <h3><p><code>$setViewValue(value);</code></p>

</h3>
    <div><p>Update the view value.</p>
<p>This method should be called when the view value changes, typically from within a DOM event handler.
For example <a href="api/ng/directive/input">input</a> and
<a href="api/ng/directive/select">select</a> directives call it.</p>
<p>It will update the $viewValue, then pass this value through each of the functions in <code>$parsers</code>,
which includes any validators. The value that comes out of this <code>$parsers</code> pipeline, be applied to
<code>$modelValue</code> and the <strong>expression</strong> specified in the <code>ng-model</code> attribute.</p>
<p>Lastly, all the registered change listeners, in the <code>$viewChangeListeners</code> list, are called.</p>
<p>Note that calling this function does not trigger a <code>$digest</code>.</p>
</div>

    
    <h4>Parameters</h4>
    
<table class="variables-matrix input-arguments">
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Details</th>
    </tr>
  </thead>
  <tbody>
    
    <tr>
      <td>
        value
        
        
      </td>
      <td>
        <a href="" class="label type-hint type-hint-string">string</a>
      </td>
      <td>
        <p>Value from the view.</p>

        
      </td>
    </tr>
    
  </tbody>
</table>

    

    
    
    

  </li>
  </ul>
  
  
<h2>Properties</h2>
<ul class="properties">
  <li id="$viewValue">
    <h3><code>$viewValue</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-string">string</a></td>
    <td><p>Actual string value in the view.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$modelValue">
    <h3><code>$modelValue</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-object">*</a></td>
    <td><p>The value in the model, that the control is bound to.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$parsers">
    <h3><code>$parsers</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-array">Array.&lt;Function&gt;</a></td>
    <td><p>Array of functions to execute, as a pipeline, whenever
       the control reads value from the DOM.  Each function is called, in turn, passing the value
       through to the next. The last return value is used to populate the model.
       Used to sanitize / convert the value as well as validation. For validation,
       the parsers should update the validity state using
       <a href="api/ng/method/$setValidity">$setValidity()</a>,
       and return <code>undefined</code> for invalid values.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$formatters">
    <h3><code>$formatters</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-array">Array.&lt;Function&gt;</a></td>
    <td><p>Array of functions to execute, as a pipeline, whenever
       the model value changes. Each function is called, in turn, passing the value through to the
       next. Used to format / convert values for display in the control and validation.</p>
<pre><code class="lang-js">function formatter(value) {
  if (value) {
    return value.toUpperCase();
  }
}
ngModel.$formatters.push(formatter);</code></pre>
</td>
  </tr>
</table>
  </li>
  
  <li id="$viewChangeListeners">
    <h3><code>$viewChangeListeners</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-array">Array.&lt;Function&gt;</a></td>
    <td><p>Array of functions to execute whenever the
    view value has changed. It is called with no arguments, and its return value is ignored.
    This can be used in place of additional $watches against the model value.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$error">
    <h3><code>$error</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-object">Object</a></td>
    <td><p>An object hash with all errors as keys.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$pristine">
    <h3><code>$pristine</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-boolean">boolean</a></td>
    <td><p>True if user has not interacted with the control yet.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$dirty">
    <h3><code>$dirty</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-boolean">boolean</a></td>
    <td><p>True if user has already interacted with the control.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$valid">
    <h3><code>$valid</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-boolean">boolean</a></td>
    <td><p>True if there is no error.</p>
</td>
  </tr>
</table>
  </li>
  
  <li id="$invalid">
    <h3><code>$invalid</code></h3>
    <table class="variables-matrix return-arguments">
  <tr>
    <td><a href="" class="label type-hint type-hint-boolean">boolean</a></td>
    <td><p>True if at least one error on the control.</p>
</td>
  </tr>
</table>
  </li>
  </ul>



  
</div>


